package com.chinasoft.order.controller;

import com.chinasoft.common.Constant;
import com.chinasoft.common.ResultData;
import com.chinasoft.order.domain.Order;
import com.chinasoft.order.service.impl.OrderService;
import com.chinasoft.utils.JedisPoolUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("order")
public class OrderController {

    private Jedis jedis = JedisPoolUtil.getJedis();

    @Resource
    private OrderService orderService;

    @GetMapping("findAll")
    public List<Order> findAll(){
        return orderService.findAll();
    }

    @GetMapping("takeOrder")
    public ResultData takeOrder(String rid,Integer id){
        String realRid = jedis.get(rid);
        System.out.println("realRid:"+realRid);
        System.out.println("rid:"+rid);
        try {
            orderService.takeOrder(realRid,id);
            return new ResultData(Constant.CODE_ONE,Constant.TAKEORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData(Constant.CODE_ZERO,Constant.TAKEORDER_FAILED);
        }
    }

    @GetMapping("doneOrder")
    public ResultData doneOrder(String id, Date arriveTime) {
        try {
            orderService.doneOrder(id,arriveTime);
            return new ResultData(Constant.CODE_ONE,Constant.DONEORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData(Constant.CODE_ZERO,Constant.DONEORDER_FAILED);
        }
    }

}
